package cn.pconline.autoclub.entity;

import cn.pconline.autoclub.util.AppUtils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.Column;
import org.gelivable.dao.Entity;
import org.gelivable.dao.Id;
import org.gelivable.dao.Label;

@Entity(tableName = "ac_dyna_image", logChange = true, split = "Mod:dynaInfoId:10")
@Label("动态图片")
public class DynaImage {
    
    public static final int DEFAULT_MAX_IMAGE_SIZE = 9;

    @Id
    @Label("图片id")
    @Column(name = "image_id")
    private long imageId;
    @Label("动态id")
    @Column(name = "dyna_info_id")
    private long dynaInfoId;
    @Label("图片url")
    @Column(name = "url")
    private String url;
    @Label("图片宽")
    @Column(name = "width")
    private int width;
    @Label("图片高")
    @Column(name = "height")
    private int height;
    @Label("图片位置")
    @Column(name = "seq")
    private int seq;

    public long getImageId() {
        return imageId;
    }

    public void setImageId(long imageId) {
        this.imageId = imageId;
    }

    public long getDynaInfoId() {
        return dynaInfoId;
    }

    public void setDynaInfoId(long dynaInfoId) {
        this.dynaInfoId = dynaInfoId;
    }

    public String getUrl() {
        if (StringUtils.isBlank(url)) {
            return url;
        }
        if (url.indexOf("/bbs6/") == -1
                && url.indexOf("/autoclub/") == -1
                && url.indexOf("/usercenter/") == -1) {
            //非论坛、非车友会、非个人中心 上传的图片URL不显示
            return "";
        }
        return url;
    }

    public String getUrl160x120() {
        return AppUtils.converImg(getUrl(),"_1024x1024", "_160x120");
    }
    
    public String getUrl240x160() {
    	return AppUtils.converImg(getUrl(),"_1024x1024", "_240x160");
    }

    public String getUrl120x80() {
        return AppUtils.converImg(getUrl(),"_1024x1024", "_120x80");
    }
    public String getUrl400x400() {
        return AppUtils.converImg(getUrl(),"_1024x1024", "_400x400");
    }
    public String getUrl500x500() {
        return AppUtils.converImg(getUrl(),"_1024x1024", "_500x500");
    }
    public String getUrl160x107() {
        return AppUtils.converImg(getUrl(),"_1024x1024", "_160x107");
    }
    
    private String convertImgage(String image, String size) {
        if (StringUtils.isBlank(image)) {
            return image;
        }
        if (image.indexOf("_1024x1024") > 0) {
            return image.replace("_1024x1024", "_" + size);
        }
        return image;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public JSONObject toJSONObject() {
        JSONObject obj = new JSONObject();
        obj.put("dynaInfoId", this.dynaInfoId);
        obj.put("height", this.height);
        obj.put("imageId", this.imageId);
        obj.put("seq", this.seq);
        obj.put("url", this.url);
        obj.put("width", this.width);
        return obj;
    }
}
